﻿Imports System.Drawing.Printing

Public Class Reportes

    Dim documento As New PrintDocument
    Dim previo As New Windows.Forms.PrintPreviewDialog
    Dim lReportes As New Lista

    Private Sub Reportes_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If lReportes.Created Then
            lReportes.Close()
        End If
    End Sub

    Private Sub Reportes_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim origen As Point
        origen.X = Screen.GetWorkingArea(New Point).X + 20
        origen.Y = Screen.GetWorkingArea(New Point).Y + 20

        Me.Location = origen

        Carga_Datos()

        dsInfo = New DataSet
        dsInfo.ReadXml(ruta_programa & "\Info.xml")
    End Sub

    Private Sub btnPagos_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPagos.Click
        documento = New PrintDocument
        previo = New Windows.Forms.PrintPreviewDialog

        If lReportes.Created Then
            lReportes.Close()
        End If

        Dim cantidad As Integer = dsData.Tables("PAGOS").Compute("COUNT(FECHA)", "FECHA='" & Format(dtpFecha.SelectionRange.Start, "dd/MM/yyyy") & "'")

        If cantidad > 0 Then
            AddHandler documento.PrintPage, AddressOf Pagos

            Dim pr As Integer
            pr = documento.PrinterSettings.PrinterResolutions.Count

            documento.DefaultPageSettings.PrinterResolution = documento.PrinterSettings.PrinterResolutions.Item(pr - 1)
            documento.DefaultPageSettings.Color = False


            documento.PrinterSettings.PrinterName = Valor_Config("EMPRESA", "Impresora Reportes")

            documento.DefaultPageSettings.Landscape = False


            previo.Document = documento
            previo.ShowDialog()
        Else
            msg.Mostrar("Nada que imprimir", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub

    Public Sub Pagos(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
        'e.Graphics.Clear(Color.White) '

        Dim margen As Integer = 10
        Dim margen2 As Integer = documento.DefaultPageSettings.PrintableArea.Width - margen
        Dim alto As Integer = 10
        Dim altura As Integer = 12
        Dim fuente As New Font("Calibri", altura, FontStyle.Regular)
        Dim fuente2 As New Font("Calibri", altura * 2, FontStyle.Bold)
        Dim fuente3 As New Font("Calibri", CInt(altura * 1.2), FontStyle.Italic)
        Dim fuente4 As New Font("Calibri", altura, FontStyle.Bold)
        Dim sf As New StringFormat

        e.Graphics.DrawImage(New Bitmap(ruta_programa & "\Fotos\SIA.png"), New Rectangle(margen, alto, 64, 64))
        sf.Alignment = StringAlignment.Near
        e.Graphics.DrawString(Valor_Config("EMPRESA", "Nombre"), fuente2, Brushes.Black, margen + 67, alto, sf)
        sf.Alignment = StringAlignment.Far
        e.Graphics.DrawString(Format(dtpFecha.SelectionRange.Start, "dd ' de ' MMMM ' del ' yyyy"), fuente3, Brushes.Black, margen2, alto, sf)

        alto += 40
        sf.Alignment = StringAlignment.Near
        e.Graphics.DrawString("Reporte de Pagos - " & Valor_Config("EMPRESA", "Nombre del Vendedor"), fuente3, Brushes.Black, margen + 70, alto, sf)

        alto += 40

        Dim C1 As Integer = margen
        Dim C2 As Integer = C1 + 85
        Dim C3 As Integer = C2 + 105
        Dim C4 As Integer = C3 + 90

        Dim C7 As Integer = margen2
        Dim C6 As Integer = C7 - 120
        Dim C5 As Integer = C6 - 120

        Dim inicio As Integer = alto

        e.Graphics.DrawString("REFER", fuente4, Brushes.Black, C1, alto, sf)
        e.Graphics.DrawString("FECHA", fuente4, Brushes.Black, C2, alto, sf)
        e.Graphics.DrawString("# CLIENTE", fuente4, Brushes.Black, C3, alto, sf)
        e.Graphics.DrawString("NOMBRE", fuente4, Brushes.Black, C4, alto, sf)

        sf.Alignment = StringAlignment.Far
        e.Graphics.DrawString("IMPORTE", fuente4, Brushes.Black, C5, alto, sf)
        e.Graphics.DrawString("SALDO", fuente4, Brushes.Black, C6, alto, sf)
        e.Graphics.DrawString("PAGO", fuente4, Brushes.Black, C7, alto, sf)

        alto += altura + 8
        e.Graphics.DrawLine(Pens.Black, margen, alto, margen2, alto)

        alto += 3
        e.Graphics.DrawLine(Pens.Black, margen, alto, margen2, alto)

        alto += 5

        Dim pagado As Double = 0

        For Each fila As DataRow In dsData.Tables("PAGOS").Select("FECHA='" & Format(dtpFecha.SelectionRange.Start, "dd/MM/yyyy") & "'")

            sf.Alignment = StringAlignment.Near
            e.Graphics.DrawString(Trim(fila("REFER")), fuente, Brushes.Black, C1, alto, sf)
            e.Graphics.DrawString(fila("CCLIE"), fuente, Brushes.Black, C3, alto, sf)
            e.Graphics.DrawString(CType(dsInfo.Tables("CLIENTES").Select("CCLIE='" & fila("CCLIE") & "'")(0), DataRow)("NOMBRE"), fuente, Brushes.Black, C4, alto, sf)

            If fila("TIPO") = "C" Then
                e.Graphics.DrawString(CType(dsInfo.Tables("CUENTAS").Select("REFER='" & fila("REFER") & "'")(0), DataRow)("FECHA"), fuente, Brushes.Black, C2, alto, sf)

                sf.Alignment = StringAlignment.Far
                e.Graphics.DrawString(Format(CType(dsInfo.Tables("CUENTAS").Select("REFER='" & fila("REFER") & "'")(0), DataRow)("IMPORTE"), "$ ###,###,##0.00"), fuente, Brushes.Black, C5, alto, sf)
                e.Graphics.DrawString(Format(CType(dsInfo.Tables("CUENTAS").Select("REFER='" & fila("REFER") & "'")(0), DataRow)("SALDO"), "$ ###,###,##0.00"), fuente, Brushes.Black, C6, alto, sf)
            Else
                e.Graphics.DrawString(fila("FECHA"), fuente, Brushes.Black, C2, alto, sf)

                sf.Alignment = StringAlignment.Far
                e.Graphics.DrawString(Format(CType(dsData.Tables("FACTURAS").Select("REFER='" & fila("REFER") & "'")(0), DataRow)("TOTAL"), "$ ###,###,##0.00"), fuente, Brushes.Black, C5, alto, sf)
                e.Graphics.DrawString(Format(CType(dsData.Tables("FACTURAS").Select("REFER='" & fila("REFER") & "'")(0), DataRow)("TOTAL"), "$ ###,###,##0.00"), fuente, Brushes.Black, C6, alto, sf)
                'e.Graphics.DrawString(Format(0, "$ ###,###,##0.00"), fuente, Brushes.Black, C6, alto, sf)
            End If

            e.Graphics.DrawString(Format(fila("IMPORTE"), "$ ###,###,##0.00"), fuente, Brushes.Black, C7, alto, sf)

            pagado += fila("IMPORTE")

            alto += altura + 7
        Next

        Dim final As Integer = alto

        e.Graphics.DrawLine(Pens.Black, C2 - 4, inicio, C2 - 4, final)
        e.Graphics.DrawLine(Pens.Black, C3 - 4, inicio, C3 - 4, final)
        e.Graphics.DrawLine(Pens.Black, C4 - 4, inicio, C4 - 4, final)
        e.Graphics.DrawLine(Pens.Black, C4 + 195, inicio, C4 + 195, final)
        e.Graphics.DrawLine(Pens.Black, C5 + 4, inicio, C5 + 4, final)
        e.Graphics.DrawLine(Pens.Black, C6 + 4, inicio, C6 + 4, final)

        alto += 3
        e.Graphics.DrawLine(Pens.Black, C6 + 8, alto, margen2, alto)

        alto += 3
        e.Graphics.DrawLine(Pens.Black, C6 + 8, alto, margen2, alto)

        alto += 3
        e.Graphics.DrawString(Format(pagado, "$ ###,###,##0.00"), fuente4, Brushes.Black, C7, alto, sf)
    End Sub

   
    Private Sub btnPedidos_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPedidos.Click
        If lReportes.Created Then
            lReportes.Close()
        End If

        lReportes = New Lista
        lReportes.Text = "Pedidos realizados el día (" & Format(dtpFecha.SelectionRange.Start, "dd/MM/yyyy") & ")"
        lReportes.Show()

        Dim origen As Point
        origen.X = Screen.GetWorkingArea(New Point).X + 20 + Me.Width + 15
        origen.Y = Screen.GetWorkingArea(New Point).Y + 20

        lReportes.Location = origen
        lReportes.Height = Me.Height

        lReportes.carga_pedidos(dtpFecha.SelectionRange.Start)

        AddHandler lReportes.dgLista.CellClick, AddressOf dgLista_CellClick

    End Sub

    Private Sub dgLista_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs)
        Variables.Imprime_Pedido(CType(sender, DataGridView).Rows(e.RowIndex).Cells("REFER").Value, True)
    End Sub
End Class